// C Source File
// Created 11/15/2007; 2:29:01 PM

#include <tigcclib.h>

// c++ type bool
typedef unsigned short bool;

// define the global constants
#define SPRITE_HEIGHT	9
#define CURSOR_HEIGHT	11

//define more key constants
#define SUPER_LEFT	4434
#define SUPER_RIGHT	4440
#define SUPER_UP	  4433
#define SUPER_DOWN  4436
#define KEY_1 49
#define KEY_2 50
#define KEY_3 51
#define KEY_4 52
#define KEY_5 53
#define KEY_6 54
#define KEY_7 55
#define KEY_8 56

//defines the map indices
#define WATER 0
#define DESERT 1
#define GRASSLAND 2
#define PLAINS 3
#define TUNDRA 4
#define FOREST 5
#define HILLS 6
#define PEAKS 7
#define DebugStr(X) ClrScr(); DrawStr(50, 45, X, A_NORMAL); ngetchx();

char mapName[] = "civmap0";

// variables for different quadrents of the map
int qX = 3, qY = 4;

//an array for the cursor coordinates
int cursor[2] = {5,3};
int cursorSize = 1;
int halfSize = 0;

// Creates the sprites for the background
unsigned short int backpic[8][9] = {{0xFFC0,0xFFC0,0xD5C0,0xABC0,0xFFC0,0xEAC0,0xD5C0,0xFFC0,0xFFC0},  //water
																  	{0x0040,0x0040,0x0040,0x0040,0x0040,0x0040,0x0040,0x0040,0x0040},  //desert
																		{0x0040,0x5540,0x5540,0x0040,0x2A40,0x2A40,0x0040,0x5540,0x5540},  //grassland
																		{0x4440,0x5540,0x1140,0x0040,0x0040,0x0040,0x4440,0x5540,0x1140},  //plains
																		{0x3840,0x4440,0x8240,0x1C40,0x2240,0x4140,0x0E40,0x1140,0x20C0},  //tundra
																		{0x2240,0x7740,0xFFC0,0x2240,0x0040,0x2240,0x7740,0xFFC0,0x2240},  //forest
																		{0x0040,0x0040,0x0040,0x0040,0x0040,0x3C40,0x6640,0xC340,0x81C0},  //hills
																		{0x0840,0x1C40,0x1440,0x3640,0x2240,0x6340,0x4140,0xC1C0,0x80C0}}; //peaks

// Creates the sprite for the cursor
unsigned short int cursorpic[11] = {0xAAA0,0x0000,0x8020,0x0000,0x8020,0x0000,0x8020,0x0000,0x8020,0x0000,0xAAA0};

// Creates arrays for strings of names of terrain
//                           1        2          3           4         5         6        7        8
char terrain_name[8][10] = {"Sea", "Desert", "Grassland", "Plains", "Tundra", "Forest", "Hills", "Peaks"};

// World Map
char *worldmap = 0;
char wWidth;
char wHeight;
int qXMax, qYMax;
#define Worldmap(X,Y) worldmap[(X)*wWidth+(Y)]

// METHODS
void DrawMap();
void DrawMapSquare(int x, int y);
void DrawMiniMap();
void DrawCursor();
void MoveMainCursor();
void ClrGrid(int x, int y);
void CreateMap();
void SaveMap();
void LoadMap();
void ApplyToMap(int index);
void MainMenu();
bool ConfirmOverwrite();
void GetName(char *name);

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// DrawMap ////////////////////////////////////////////////////////////////////////////////////////////////////////
// function to draw the map ///////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void DrawMap()
{		
	  // clear the screen
		ClrScr();
		
		// makes a grid
		int loopA, loopB;
		for (loopA = 0; loopA < 111; loopA+=10)
		for (loopB = 0; loopB < 71; loopB+=10) 
		{		
			// Draw the line for the grid
			DrawLine(0, 29+loopB, 110, 29+loopB, A_NORMAL);
			DrawLine(loopA, 29, loopA, 99, A_NORMAL);
		}
		
		for (loopA = 0; loopA < 11; loopA++)
		for (loopB = 0; loopB < 7; loopB++) 
		{
			// Draw the background sprite
			Sprite16(10*loopA+1, 10*loopB+30, SPRITE_HEIGHT, backpic[Worldmap(3*qY+loopB,5*qX+loopA)-1], LCD_MEM, SPRT_OR);
		}
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// DrawMapSquare //////////////////////////////////////////////////////////////////////////////////////////////////
// function to draw the map ///////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void DrawMapSquare(int x, int y)
{		
	// Draw the background sprite
	Sprite16(10*(x - 5*qX -1)+1, 10*(y - 3*qY -1)+30, SPRITE_HEIGHT, backpic[Worldmap(3*qY+y-1,5*qX+x-1)-1], LCD_MEM, SPRT_OR);
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// DrawMiniMap ////////////////////////////////////////////////////////////////////////////////////////////////////
// draws the entire map, showing current position /////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void DrawMiniMap() 
{
	// clear the screen
	ClrScr();
	
	int loopA, loopB, count;
	for (loopA = 0; loopA < wWidth; loopA++)
	for (loopB = 0; loopB < wHeight; loopB++)
		if (Worldmap(loopB,loopA) < 2)
			DrawPix(31 + loopA, 27 + loopB, A_NORMAL);
			
	DrawPix(30 + cursor[0] + 5*qX, 26 + cursor[1] + 3*qY, A_XOR);
	
	while (!kbhit()) {
		for (count = 1; count < 1500; count++) kbhit();
		DrawPix(30 + cursor[0] + 5*qX, 26 + cursor[1] + 3*qY, A_XOR);
	}
	
	GKeyFlush();
}
	
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// ClrGrid ////////////////////////////////////////////////////////////////////////////////////////////////////////	 																
// clears a grid for placement ////////////////////////////////////////////////////////////////////////////////////	
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////		
void ClrGrid(int x, int y) 
{	
	int loopA, loopB;
	
	for (loopA = 0; loopA < 9; loopA++)
	for (loopB = 0; loopB < 9; loopB++)
		DrawPix(10*(x - 5*qX -1)+1 + loopA, 10*(y - 3*qY -1)+30 + loopB, A_REVERSE);
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// DrawCursor /////////////////////////////////////////////////////////////////////////////////////////////////////
// draws the cursor so select units and buildings, and to move the screen /////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void DrawCursor() 
{
	int i,j;
	
	for(i = -halfSize; i < halfSize+1; i++)
	for(j = -halfSize; j < halfSize+1; j++)
		Sprite16(10*(cursor[0] - 1 + i), 10*(cursor[1] - 1 + j)+29, CURSOR_HEIGHT, cursorpic, LCD_MEM, SPRT_XOR);
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// ApplyToMap /////////////////////////////////////////////////////////////////////////////////////////////////////
// changes the map at cursor location /////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void ApplyToMap(int index)
{
	int i,j;
	
	for(i = -halfSize+cursor[0]; i < halfSize+1+cursor[0]; i++)
	for(j = -halfSize+cursor[1]; j < halfSize+1+cursor[1]; j++)
		Worldmap(3*qY+j-1,5*qX+i-1) = index + 1;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// MoveMainCursor /////////////////////////////////////////////////////////////////////////////////////////////////
// moves the cursor and handles the keys for main loop ////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void MoveMainCursor()
{
	bool new_screen = 1;
	int loop, key = 0;
	
	while (key != KEY_ESC) 
	{		
		//reset the key var
		key = 0;
		
		//draws the screen if new_screen == (true)
		if (new_screen)
		{
			DrawMap();
			
			DrawStr(3,3,"F1 = 1x1 Cursor", A_NORMAL);
			DrawStr(3,11,"F2 = 3x3 Cursor", A_NORMAL);
			DrawStr(3,19,"F3 = 5x5 Cursor", A_NORMAL);
			
			DrawStr(60, 3, "F4 = Save Map", A_NORMAL);
			DrawStr(60, 11, "F5 = MiniMap", A_NORMAL);
			
			DrawStr(113, 32, "1 - Sea", A_NORMAL);
			DrawStr(113, 40, "2 - Desert", A_NORMAL);
			DrawStr(113, 48, "3 - Grassland", A_NORMAL);
			DrawStr(113, 56, "4 - Plains", A_NORMAL);
			DrawStr(113, 64, "5 - Tundra", A_NORMAL);
			DrawStr(113, 72, "6 - Forest", A_NORMAL);
			DrawStr(113, 80, "7 - Hills", A_NORMAL);
			DrawStr(113, 88, "8 - Peaks", A_NORMAL);
		}
		new_screen = 0;
		
		while(key == 0)
		{
			DrawCursor();
			for (loop = 1; loop < 1000 && key == 0; loop++) key = kbhit();
			DrawCursor();
			for (loop = 1; loop < 1000 && key == 0; loop++) key = kbhit();
		}
		GKeyFlush();
		
		switch (key)
		{    
	    case KEY_LEFT: //left
	      if (--cursor[0]-halfSize < 1)
	      	if (qX > 0) { qX--;cursor[0] = 6+halfSize;new_screen = 1;}
	      	else cursor[0] = 1+halfSize;
	      break;
	      
	    case SUPER_LEFT: //2nd + left
	      if (qX > 0) { qX--;new_screen = 1; }
	      else cursor[0] = 1+halfSize;
	      break;
	      
	    case KEY_RIGHT: //right
	      if (++cursor[0]+halfSize > 11)
	      	if (qX < qXMax) { qX++;cursor[0] = 6-halfSize;new_screen = 1; }
	      	else cursor[0] = 11-halfSize;
	      break;
	      
	    case SUPER_RIGHT: //2nd + right
	      if (qX < qXMax) { qX++;new_screen = 1; }
	      else cursor[0] = 11-halfSize;
	      break;  
	      
	    case KEY_UP: //up
	      if (--cursor[1]-halfSize < 1)
	      	if (qY > 0) { qY--;cursor[1] = 4+halfSize;new_screen = 1; }
	      	else cursor[1] = 1+halfSize;
	      break;
	    
	    case SUPER_UP: //2nd + up
	      if (qY > 0) { qY--;new_screen = 1; }
	      else cursor[1] = 1+halfSize;
	      break;
	      
	    case KEY_DOWN: //down
	      if (++cursor[1]+halfSize > 7)
	      	if (qY < qYMax) { qY++;cursor[1] = 4-halfSize;new_screen = 1; }
	      	else cursor[1] = 7-halfSize;
	      break;
	    
	    case SUPER_DOWN: //2nd + down
	      if (qY < qYMax) { qY++;new_screen = 1; }
	      else cursor[1] = 7-halfSize;
	      break;
	      
	    case KEY_1:
	    	ApplyToMap(0);
	    	new_screen = 1;
	    	break;
	      
	    case KEY_2:
	    	ApplyToMap(1);
	    	new_screen = 1;
	    	break;
	      
	    case KEY_3:
	    	ApplyToMap(2);
	    	new_screen = 1;
	    	break;
	      
	    case KEY_4:
	    	ApplyToMap(3);
	    	new_screen = 1;
	    	break;
	      
	    case KEY_5:
	    	ApplyToMap(4);
	    	new_screen = 1;
	    	break;
	      
	    case KEY_6:
	    	ApplyToMap(5);
	    	new_screen = 1;
	    	break;
	      
	    case KEY_7:
	    	ApplyToMap(6);
	    	new_screen = 1;
	    	break;
	      
	    case KEY_8:
	    	ApplyToMap(7);
	    	new_screen = 1;
	    	break;
	      
	    case KEY_F1:
	      cursorSize = 1;
	      halfSize = 0;
	      break;
	      
	    case KEY_F2:
	      cursorSize = 3;
	      halfSize = 1;
	      break;
	      
	    case KEY_F3:
	      cursorSize = 5;
	      halfSize = 2;
	      break;
	      
	    case KEY_F4:
	    	SaveMap();
	    	new_screen = 1;
	    	break;
	      
	    case KEY_F5:
	      DrawMiniMap();
  			new_screen = 1;
  			kbhit();
	      break;
	      
	    //default:
	    //	sprintf(keyStr, "key: %d", key);
	    //	DrawStr(110,3,keyStr,A_NORMAL);
	    //	break;
	      
	    //case KEY_ENTER:
	      //if(Select()) new_screen = 1;
	      //break;
  	}
  
	}
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// CreateMap //////////////////////////////////////////////////////////////////////////////////////////////////////
// creates a new map or loads old one /////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void CreateMap()
{
	HANDLE dlg = H_NULL, menu = H_NULL;
	short int option = 2;
	
	if((dlg = DialogNewSimple(90,35)) != H_NULL)
	{
		if((menu = PopupNew(NULL,0)) != H_NULL)
		{
	    PopupAddText(menu,-1,"Small",  1);
	    PopupAddText(menu,-1,"Medium", 2);
	    PopupAddText(menu,-1,"Large",  3);
	    
	    DialogAddTitle(dlg,"Map Size",BT_OK,BT_NONE);
    	DialogAddPulldown(dlg,5,15,"Size:",menu,0);
    
	    int key = 0;
	    while (key != KEY_ENTER && key != KEY_ESC) 
	    	key = DialogDo(dlg,CENTER,CENTER,NULL,&option);
			FontSetSys(F_4x6);
	    
	    if(key == KEY_ESC) return;
	    else if(option == 1)
	    {
	    	wHeight = 37;
	    	wWidth = 61;
	    	qXMax = 10;
	    	qYMax = 10;
	    }
	    else if(option == 2)
	    {
	    	wHeight = 43;
	    	wWidth = 91;
	    	qXMax = 16;
	    	qYMax = 12;
	    }
	    else if(option == 3)
	    {
	    	wHeight = 49;
	    	wWidth = 111;
	    	qXMax = 20;
	    	qYMax = 14;
	    }
	    
			worldmap = (char*)malloc(wWidth * wHeight * sizeof(char));
			int i;
			for(i = 0; i < wWidth * wHeight; i++)
				worldmap[i] = 1;
			MoveMainCursor();
		}
	}
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// ConfirmOverwrite ///////////////////////////////////////////////////////////////////////////////////////////////
// confirms the overwrite of a map file ///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool ConfirmOverwrite()
{
	HANDLE dlg = H_NULL;
	bool isValid = 0;
	
	if((dlg = DialogNewSimple(140,35)) != H_NULL)
	{
   	// format the dialog box
    DialogAddTitle(dlg,"Confirm",BT_OK,BT_CANCEL);
    DialogAddText(dlg,45,15,"Overwrite current file?");
    
    int key = 0;
    while (key != KEY_ENTER && key != KEY_ESC) key = DialogDo(dlg,CENTER,CENTER,NULL,NULL);
    if(key == KEY_ENTER) isValid = 1;
    
		FontSetSys(F_4x6);
	} 
	
	return isValid;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// GetName ////////////////////////////////////////////////////////////////////////////////////////////////////////
// gets a name for the map save file //////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void GetName(char *name)
{
	HANDLE dlg = H_NULL;
	
	if((dlg = DialogNewSimple(140,35)) == H_NULL)
		return;
		
  // format the dialog box
  DialogAddTitle(dlg,"Save Map",BT_OK,BT_NONE);
  DialogAddRequest(dlg,5,15,"Name:",0,15,16);
    
  int key = 0;
  while (key != KEY_ENTER && key != KEY_ESC) key = DialogDo(dlg,CENTER,CENTER,name,NULL);
    
	FontSetSys(F_4x6);
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// WriteFile //////////////////////////////////////////////////////////////////////////////////////////////////////
// writes a file to memory ////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void SaveMap()
{
	int key = 0;
	int map = 0;
	int i;
	
	char number[2] = "0";
	char name[20];
	char width[15]; char w;
	char height[15]; char h;
	
	while(key != KEY_ESC && key != KEY_ENTER)
	{
		ClrScr();
		FontSetSys(F_8x10);
		for(i = 0; i < 10; i++)
		{
			DrawLine(5 + 15*i,  4, 15 + 15*i,  4, A_NORMAL);
			DrawLine(5 + 15*i, 16, 15 + 15*i, 16, A_NORMAL);
			DrawLine(5 + 15*i,  4,  5 + 15*i, 16, A_NORMAL);
			DrawLine(15 + 15*i, 4, 15 + 15*i, 16, A_NORMAL);
			
			number[0] = i + '0';
			DrawStr(6 + 15*i, 6, number, A_NORMAL);
		}
		
		DrawLine(4 + 15*map,  3, 16 + 15*map,  3, A_NORMAL);
		DrawLine(4 + 15*map, 17, 16 + 15*map, 17, A_NORMAL);
		DrawLine(4 + 15*map,  3,  4 + 15*map, 17, A_NORMAL);
		DrawLine(16+ 15*map,  3, 16 + 15*map, 17, A_NORMAL);

		// open file for writing
		mapName[6] = '0' + map;
		FILE *f = fopen(mapName,"rb");
		bool empty = 0;
		
		if(f == NULL)
		{
			FontSetSys(F_6x8);
			DrawStr(5, 28, "Empty", A_NORMAL);
			FontSetSys(F_4x6);
			empty = 1;
		}
		else 
		{
			// write structure data to the file
			fread(name, 20*sizeof(char),1,f);
			fread(&w,sizeof(char),1,f);
			fread(&h,sizeof(char),1,f);
	
			// close the file
			fclose(f);
			
			sprintf(width, "Width: %d", w);
			sprintf(height, "Height: %d", h);
			
			FontSetSys(F_6x8);
			DrawStr(5, 28, name, A_NORMAL);
			
			FontSetSys(F_4x6);
			DrawStr(5, 45, width, A_NORMAL);
			DrawStr(5, 55, height, A_NORMAL);
			empty = 0;
		}
		
		key = ngetchx();
		
		if(key == KEY_LEFT && --map < 0) map = 9;
		else if(key == KEY_RIGHT && ++map > 9) map = 0;
		else if(key == KEY_ENTER)
		{
			if(empty || ConfirmOverwrite())
			{
				//DebugStr("Before GetName");
				// Get a name for map
				GetName(name);
				//DebugStr("After GetName");
				
				// open file for writing
				if ((f = fopen(mapName,"wb")) == NULL) 
					return;
					
				// write structure data to the file
				fwrite(name, 20*sizeof(char),1,f);
				fwrite(&wWidth,sizeof(char),1,f);
				fwrite(&wHeight,sizeof(char),1,f);
				fwrite(worldmap,wWidth*wHeight*sizeof(char),1,f);
			
				// append the file ID tag
				fputc(0,f);
				fputs("civ",f);
				fputc(0,f);
				fputc(OTH_TAG,f);
				
				// close the file
				fclose(f);
				
				ClrScr();
				DrawStr(50, 45, "File Saved.", A_NORMAL);
				ngetchx();
			}
			else key = 0;
		}
	}
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// WriteFile //////////////////////////////////////////////////////////////////////////////////////////////////////
// writes a file to memory ////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void LoadMap()
{
	int key = 0;
	int map = 0;
	int i;
	
	char number[2] = "0";
	char name[20];
	char width[15]; char w;
	char height[15]; char h;
	
	while(key != KEY_ESC && key != KEY_ENTER)
	{
		ClrScr();
		FontSetSys(F_8x10);
		for(i = 0; i < 10; i++)
		{
			DrawLine(5 + 15*i,  4, 15 + 15*i,  4, A_NORMAL);
			DrawLine(5 + 15*i, 16, 15 + 15*i, 16, A_NORMAL);
			DrawLine(5 + 15*i,  4,  5 + 15*i, 16, A_NORMAL);
			DrawLine(15 + 15*i, 4, 15 + 15*i, 16, A_NORMAL);
			
			number[0] = i + '0';
			DrawStr(6 + 15*i, 6, number, A_NORMAL);
		}
		
		DrawLine(4 + 15*map,  3, 16 + 15*map,  3, A_NORMAL);
		DrawLine(4 + 15*map, 17, 16 + 15*map, 17, A_NORMAL);
		DrawLine(4 + 15*map,  3,  4 + 15*map, 17, A_NORMAL);
		DrawLine(16+ 15*map,  3, 16 + 15*map, 17, A_NORMAL);

		// open file for writing
		mapName[6] = '0' + map;
		FILE *f = fopen(mapName,"rb");
		bool load = 0;
		
		if(f == NULL)
		{
			FontSetSys(F_6x8);
			DrawStr(5, 28, "Empty", A_NORMAL);
			FontSetSys(F_4x6);
		}
		else 
		{
			// write structure data to the file
			fread(name, 20*sizeof(char),1,f);
			fread(&w,sizeof(char),1,f);
			fread(&h,sizeof(char),1,f);
			if(worldmap) free(worldmap);
			worldmap = (char*)malloc(wWidth * wHeight * sizeof(char));
			fread(worldmap,wWidth*wHeight*sizeof(char),1,f);
			load = 1;
			
			wWidth = w;
			wHeight = h;
    	qXMax = (w - 11) / 5;
    	qYMax = (h - 7) / 3;
	
			// close the file
			fclose(f);
			
			sprintf(width, "Width: %d", w);
			sprintf(height, "Height: %d", h);
			
			FontSetSys(F_6x8);
			DrawStr(5, 28, name, A_NORMAL);
			
			FontSetSys(F_4x6);
			DrawStr(5, 45, width, A_NORMAL);
			DrawStr(5, 55, height, A_NORMAL);
		}
		
		key = ngetchx();
		
		if(key == KEY_LEFT && --map < 0) map = 9;
		else if(key == KEY_RIGHT && ++map > 9) map = 0;
		else if(key == KEY_ENTER && load == 0)
		{
			key = 0;
		}
	}
	
	if(key == KEY_ENTER)
	{
		MoveMainCursor();
	}
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// MainMenu ///////////////////////////////////////////////////////////////////////////////////////////////////////
// the main menu for map edit /////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void MainMenu()
{
	HANDLE dlg = H_NULL, menu = H_NULL;
	short int option = 1;
	
	if((dlg = DialogNewSimple(90,35)) != H_NULL)
	{
		if((menu = PopupNew(NULL,0)) != H_NULL)
		{
	    PopupAddText(menu,-1,"New Map",  1);
	    PopupAddText(menu,-1,"Load Map", 2);
	    PopupAddText(menu,-1,"Exit",     4);
	    
	    DialogAddTitle(dlg,"Main Menu",BT_OK,BT_NONE);
    	DialogAddPulldown(dlg,5,15,"Action:",menu,0);
    
	    int key = 0;
	    while (key != KEY_ENTER && key != KEY_ESC) 
	    	key = DialogDo(dlg,CENTER,CENTER,NULL,&option);
			FontSetSys(F_4x6);
	    
	    if(key == KEY_ESC || option == 4) return;
	    else if(option == 1) CreateMap();
	    else if(option == 2) LoadMap();
		}
	} 
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Main Function //////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void _main(void)
{
	MainMenu();	
	if(worldmap != 0)
		free(worldmap);
}
